﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mooc
{
    public partial class StuApp : Form
    {
        public StuApp(Home home, Stu stu)
        {
            InitializeComponent();
            this.home = home;
            this.stu = stu;
        }

        Home home;

        Stu stu;

        SqlDao sqlDao = new SqlDao();

        bool e1 = true;
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex==1)
            {
                DataSet ds = sqlDao.selectWorkedById(stu.S_id, 0);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;
                    if (e1)
                    {
                        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                        column.Text = "修 改";
                        column.Name = "编 辑";
                        column.UseColumnTextForButtonValue = true;
                        dataGridView1.Columns.Add(column);
                        dataGridView1.Columns[3].Width = 400;
                        e1 = false;
                    }
                }
            }
            else if(e.TabPageIndex==2)
            {
                DataSet ds = sqlDao.selectWorkById(stu.S_id, 1);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView2.DataSource = ds.Tables[0].DefaultView;
                }
            }
            else if(e.TabPageIndex==3)
            {
                if (MessageBox.Show("确定要退出...", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    home.openForm(new Login(home));
            }
            
        }

        private void submit_Click(object sender, EventArgs e)
        {
            if (content.Text == "")
            {
                MessageBox.Show("作业不能为空...");
            }
            else
            {
                Work work = new Work();
                work.W_id = Utils.getTimeTicks();
                work.S_id = stu.S_id;
                work.W_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                work.W_content = content.Text;
                if(sqlDao.insertWork(work))
                {
                    MessageBox.Show("作业提交成功...");
                    content.Text = "";
                }
                else
                {
                    MessageBox.Show("作业提交失败...");
                }
            }
        }

        int i;
        public void update(string text)
        {
            dataGridView1.Rows[i].Cells["作业内容"].Value = text;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == 0)
            {
                i = e.RowIndex;
                Work work = new Work();
                work.W_id = dataGridView1.Rows[e.RowIndex].Cells["作业ID"].Value.ToString();
                work.W_content = dataGridView1.Rows[e.RowIndex].Cells["作业内容"].Value.ToString();
                work.S_id = stu.S_id;
                UpdateWorked updateWorked = new UpdateWorked(work, this);
                updateWorked.ShowDialog();
            }
        }
    }
}
